/**
 * 
 */
package optimization.naturalOptimization.fitness;

import multithreading.FitnessCalculator;
import optimization.naturalOptimization.population.Population;

/**
 * Test fitness function to test algorithms.
 * f(x)=sum<sub>i=1</sub><sup>n-1</sup>
 * [(1-x<sub>i</sub>)<sup>2</sup>+100*(x<sub>
 * i+1</sub>-x<sub>i</sub><sup>2</sup>)<sup>2</sup>] This function has a minimum
 * at value 1 in every dimension. The fitness value in optimum is 0.
 * 
 * @see <a href="http://en.wikipedia.org/wiki/Rosenbrock_function">Rosenbrock
 *      Function</a>
 * @author Kevin Wagner
 * @version 1.0
 */
public class Rosenbrock extends SimpleFitnessFunction<double[]> {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * optimization.naturalOptimization.fitness.SimpleFitnessFunction#getFitness
	 * (java.lang.Object)
	 */
	@Override
	public double getFitness(double[] position) {
		double erg = 0.0;

		for (int i = 0; i < position.length - 1; i++) {
			erg += (1 - position[i]) * (1 - position[i]) + 100
					* (position[i + 1] - position[i] * position[i])
					* (position[i + 1] - position[i] * position[i]);
		}
		return erg;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see optimization.IdentificationI#getIndentification()
	 */
	@Override
	public String getIndentification() {
		return "Test Function: Rosenbrock's function";
	}

	@Override
	public void getFitness(Population pop) throws Exception{
		int ind=pop.getNumberOfIndividuals();
		FitnessCalculator[] calculators=new FitnessCalculator[ind];
		for(int i=0;i<ind;i++){
			calculators[i]=new FitnessCalculator(pop.getIndividual(i), this);
			calculators[i].start();
		}
		for(int i=0;i<ind;i++){
				calculators[i].join();
		}
	}

}
